In [1]:
# Questions 1

In [2]:
import sqlite3
conn = sqlite3.connect('movie.db')
cur = conn.cursor()

a) Which thrillers were directed by Steven Spielberg?


In [3]:
cur.execute('''SELECT film.title
               FROM film, person, participation 
               WHERE film.genre LIKE '%Thriller%' 
               AND film.id = participation.film 
               AND person.id = participation.person
               AND participation.function = "director"
               AND person.name= "Spielberg"
               AND person.firstname = "Steven" ''')
for row in cur.fetchall():
    print(row[0])


Bridge of Spies
Minority Report
Jaws
Munich
Duel

b) Who acted in at least 20 different films?


In [4]:
cur.execute('''SELECT DISTINCT person.firstname, person.name
               FROM person, participation
               WHERE person.id = participation.person
               AND participation.person IN 
                   (SELECT  participation.person
                    FROM participation
                    GROUP BY participation.person 
                    HAVING count(*)>20);''')

cur.execute('''SELECT person.firstname, person.name
               FROM (person JOIN participation ON participation.person=person.ID)
               GROUP BY person.ID
               HAVING COUNT(participation.film) > 20;''')

# Both statements work.

for row in cur.fetchall():
    print(row[0], row[1])


Johnny Depp
Tom Hanks
Clint Eastwood
Samuel L. Jackson
Frank Welker
Brad Pitt
Robert De Niro
Dustin Hoffman

c) List all shows of “Alice in Wonderland”.


In [5]:
cur.execute('''SELECT show.date, cinema.name, cinema.city
               FROM show, film, cinema
               WHERE show.film = film.id
               AND show.cinema = cinema.id
               AND film.title="Alice in Wonderland";''')
for row in cur.fetchall():
    print(row[0], row[1], row[2])


1982-06-24 12:58:27 Off Broadway Kino Cologne
1985-04-11 05:49:45 Cinenova Cologne
1985-07-13 08:13:03 Theater am Weißhaus Cologne
1987-03-18 17:12:04 Rex-Lichtspieltheater Bonn
1987-12-18 13:16:49 WOKI - Dein Kino! Bonn
1993-05-23 00:28:52 Stern Lichtspiele Bonn
1994-08-18 23:33:24 ODEON-Lichtspieltheater GmbH Cologne
1997-02-10 17:50:12 Metropolis Lichtspieltheater GmbH Cologne
2000-08-22 15:06:19 Neue Filmbühne Bonn
2003-03-02 09:31:39 Filmpalette Cologne
2009-01-31 03:11:25 Cinedom Cologne
2012-02-02 06:41:45 Residenz Cologne
2012-05-09 07:50:56 Bonner Kinemathek Bonn

d) Who acted in his/her own movie?


In [6]:
cur.execute('''SELECT p.firstname, p.name, f.title
               FROM (((person p INNER JOIN participation par
               ON p.ID=par.person AND par.function="director") INNER JOIN film f
               ON par.film=f.ID) INNER JOIN participation par2
               ON p.ID=par2.person AND par2.film=par.film AND par2.function="actor")
               ORDER BY p.name;''')

'''
Some of the results seem to be counter intuitive as actors in animation movies 
or severall people acting and directing in the same movie. But this is due to 
co-directors and the voice cast of animation movies
'''
for row in cur.fetchall()[:20]:
    print(row[0], row[1], row[2])


Jim Abrahams Airplane!
Andrew Adamson Shrek
Ben Affleck Argo
Ben Affleck The Town
Woody Allen Café Society
Woody Allen Annie Hall
Woody Allen Hannah and Her Sisters
Woody Allen Sleeper
Woody Allen Manhattan
Woody Allen Manhattan Murder Mystery
Pedro Almodóvar La mala educación
Wes Anderson Fantastic Mr. Fox
Stephen J. Anderson Meet the Robinsons
Darren Aronofsky Requiem for a Dream
Yann Arthus-Bertrand Home
Alexandre Astier Astérix: Le domaine des dieux
Aldo Baglio Tre uomini e una gamba
Kyle Balda Minions
Elizabeth Banks Pitch Perfect 2
Drew Barrymore Whip It

e) Which cinemas show films with Kate Winslet?


In [12]:
cur.execute('''SELECT DISTINCT c.name, c.city
               FROM (cinema c INNER JOIN show s
               ON c.ID=s.cinema)
               WHERE s.film IN 
               
               (SELECT f.ID
               FROM (film f INNER JOIN participation par
               ON f.ID= par.film)
               WHERE par.person=
               
               (SELECT p.ID
               FROM person p
               WHERE p.name="Winslet" AND p.firstname="Kate"))
               ;''')

cur.execute('''SELECT DISTINCT c.name, c.city
               FROM (((cinema c JOIN show s ON c.ID=s.cinema)
                                JOIN participation par ON s.film = par.film)
                                JOIN person p ON p.ID=person)
               
               WHERE p.name="Winslet" AND p.firstname="Kate"
               ;''')

for row in cur.fetchall()[:20]:
    print(row)


('Metropolis Lichtspieltheater GmbH', 'Cologne')
('Bonner Kinemathek', 'Bonn')
('Filmpalette', 'Cologne')
('Neue Filmbühne', 'Bonn')
('Cinenova', 'Cologne')
('WOKI - Dein Kino!', 'Bonn')
('Off Broadway Kino', 'Cologne')
('Residenz', 'Cologne')
('Cinedom', 'Cologne')
('Theater am Weißhaus', 'Cologne')
('Stern Lichtspiele', 'Bonn')
('Rex-Lichtspieltheater', 'Bonn')
('ODEON-Lichtspieltheater GmbH', 'Cologne')

f) Which films have more than one director?


In [16]:
cur.execute('''SELECT DISTINCT f.title
               FROM ((film f INNER JOIN participation par
               ON f.ID = par.film AND par.function='director') INNER JOIN participation par1
               ON f.ID = par1.film AND par1.function='director' AND par.person IS NOT par1.person)
               ;''')

cur.execute('''SELECT f.title
               FROM (film f  JOIN participation par ON f.ID = par.film)
               WHERE par.function='director'
               GROUP BY ID
               HAVING COUNT(*) > 1
               ORDER BY f.title asc
               ;''')

for row in cur.fetchall()[:20]:
    print(*row)


21 Jump Street
22 Jump Street
A Bug's Life
A Goofy Movie
African Cats
Airplane!
Aladdin
Alice in Wonderland
Anastasia
Anomalisa
Arthur Christmas
Astérix et Cléopâtre
Astérix et la surprise de César
Astérix: Le domaine des dieux
Babysitting
Babysitting 2
Bambi
Batman: Assault on Arkham
Batman: Year One
Bears

g) Which films have not been presented in a cinema yet?


In [ ]:
cur.execute('''SELECT f.title
               FROM (film f JOIN show s 
               ON f.ID=s.film)
               WHERE s.date > '2015-05-30'
               ;''')

'''The dates have been assigned randomly between 1980-01-01 and 2016-01-01 during database creation. '''

for row in cur.fetchall()[:20]:
    print(*row)

h) Who hasn’t participated in a film yet?


In [ ]:
cur.execute('''SELECT p.firstname, p.name
               FROM person p
               
               EXCEPT 
               SELECT p.firstname, p.name
               FROM (person p JOIN participation par 
               ON p.ID=par.person)
               ORDER BY p.name, p.firstname
               ;''')

# It seems that for severall actors no participation record was written

for row in cur.fetchall()[:20]:
    print(*row)

In [ ]:
cur.execute('''SELECT p.firstname, p.name
               FROM person p
               WHERE p.ID NOT IN (SELECT par.person
                             FROM participation par)
               ORDER BY p.name, p.firstname
               ;''')


for row in cur.fetchall()[:20]:
    print(*row)

i) Who directed at least two different films in the same year?


In [ ]:
cur.execute('''SELECT p.firstname, p.name
               FROM person p 
               WHERE p.ID IN (
               
               SELECT x.person
               FROM (
               (SELECT *
                FROM (film f JOIN participation par
                ON f.ID=par.film) 
                WHERE par.function="director" ) as x
                
               JOIN 
               
               (SELECT *
                FROM (film f1 JOIN participation par1
                ON f1.ID=par1.film) 
                WHERE par1.function="director" ) as y
                
               ON x.year=y.year 
               AND x.person = y.person 
               AND x.film <> y.film
               ))
               
               ;''')


for row in cur.fetchall()[:100]:
    print(*row)

In [ ]:
cur.execute('''SELECT f.year, f.title
               FROM (( film f JOIN participation par
               ON f.ID=par.film) JOIN person p ON p.ID=par.person)
               WHERE p.name = "Donner"
               AND p.firstname = "Richard"
               AND par.function='director'
               Order By f.year
               
               ;''')

# Just to see which movies where made in the same year

for row in cur.fetchall()[:100]:
    print(*row)

k) Are there persons having the same name (name and first name)?


In [ ]:
cur.execute('''SELECT DISTINCT p.firstname, p.name
               FROM person p JOIN person p1
               ON p.name = p1.name 
               AND p.firstname = p1.firstname  
               AND p.ID <> p1.ID
               ORDER BY p.name, p.firstname
               ;''')

# Just to see which movies where made in the same year

for row in cur.fetchall()[:20]:
    print(*row)

Question 2

What is the meaning of the following SQL queries over the film schema. Provid the corresponding realational algebra expressions.

a)

SELECT DISTINCT title
FROM (film JOIN show ON ID = film) 
JOIN cinema ON cinema.ID = cinema
WHERE name = ’Metropol’

b)

SELECT DISTINCT person.name, person.firstname
FROM film, person, cinema, participation, show
WHERE film.ID = participation.film 
AND film.ID = show.film
AND person.ID = person 
AND cinema.ID = cinema
AND date = ’2016-11-16’

Sheet 5

Question 3


In [ ]:
cur.execute('''SELECT DISTINCT p.firstname, p.name
               FROM (((person p JOIN participation par
               ON p.ID=par.person) JOIN film f
               ON par.film=f.ID) JOIN show s
               ON f.ID=s.film)
               WHERE s.date<"2017-01-01"
               ;''')

# Just to see which movies where made in the same year

for row in cur.fetchall()[:20]:
    print(*row)

In [ ]:
cur.execute('''SELECT DISTINCT p.firstname, p.name
               FROM person p 
               WHERE EXISTS (SELECT par.person
                             FROM (participation par JOIN show s
                             ON s.film=par.film )
                             WHERE s.date<"2017-01-01")
               
               ;''')

# Just to see which movies where made in the same year

for row in cur.fetchall()[:20]:
    print(*row)